﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="GraphicsDeviceExtensions.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2011 
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Core.
//
//   starLiGHT.Core is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Core is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Core. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Core are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev::                       $:  Revision of last commit *
// * $Author::                    $:  Author of last commit   *
// * $Date::                      $:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT
{
    #region Using Statements
    using System;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    #endregion

    /// <summary>
    /// Extension methods for XNA's GraphicsDevice
    /// </summary>
    public static class GraphicsDeviceExtensions
    {
        private static bool resetEventHandler = false;
        private static bool screenSizeDirty = true;
        private static Vector2 screenSize;

        public static Vector2 GetSize(this GraphicsDevice device)
        {
            if (!resetEventHandler)
            {
#if XNA4
                device.DeviceReset += new EventHandler<EventArgs>(device_DeviceReset);
#else
                device.DeviceReset += new EventHandler(device_DeviceReset);
#endif
                resetEventHandler = true;
            }

            if (screenSizeDirty)
            {
                screenSize = new Vector2(device.PresentationParameters.BackBufferWidth, device.PresentationParameters.BackBufferHeight);

                screenSizeDirty = false;
            }

            return screenSize;
        }

        private static void device_DeviceReset(object sender, EventArgs e)
        {
            screenSizeDirty = true;
        }
    }
}
